/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.google.code.peersim.pastry.protocol;

/**
 * This class represents messages sent in case a node needs to replace a failed
 * routing-table entry. The key stores the {@link PastryId} of the dead node.
 *
 * @author frusso
 * @version 0.1
 * @since 0.1
 */
class RoutingTableEntryReq extends PastryMessage {

  private int l;
  private char d;

  /**
   * Constructor.
   *
   * @param deadNodeId The dead node id
   * @param src The sender
   * @param dst The received
   * @param l The Pastry {@code L} parameter
   * @param d The Pastry {@code d} parameter
   */
  RoutingTableEntryReq(PastryId deadNodeId, PastryNode src, PastryNode dst, int l, char d) {
    super.setType(Type.ROUTING_TABLE_ENTRY_REQ);
    super.setKey(deadNodeId);
    super.setOrignator(src);
    super.setSender(src);
    super.setDestination(dst);
    this.l = l;
    this.d = d;
  }

  /**
   * Returns the Pastry {@code L} parameter.
   *
   * @return The Pastry {@code L} parameter
   */
  public int getL() {
    return l;
  }

  /**
   * Returns the Pastry {@code b} parameter.
   *
   * @return The Pastry {@code b} parameter
   */
  public char getD() {
    return d;
  }

  /**
   * Returns the dead node id.
   * @return The dead node id
   */
  public PastryId getDeadNodeId() {
    return super.getKey();
  }

  /**
   * Creates a reply message to this message.
   *
   * @param shallop The node to be used as replacement
   * @return A response message
   */
  RoutingTableEntryRsp reply(PastryNodeDescriptor shallop) {
    RoutingTableEntryRsp rsp = new RoutingTableEntryRsp(this.getDeadNodeId(), this.getDst(), this.getOriginalSender(), shallop, this.l, this.d);
    rsp.setOrignator(this.getOriginalSender());
    rsp.setHops(this.getHops());
    return rsp;
  }
}